var index = {
	init:function(){
    this.layuiUse();	
    this.goCancleOrder();
    this.goPayAgain();
	},
	layuiUse:function(){
		layui.use(['layer', 'form','jquery','element'], function(){
        var layer = layui.layer,
            form = layui.form,
            element = layui.element,
            $ = layui.$;
        
        //判断展示哪个tab
        if(window.location.href.indexOf('type=2')!=-1){
          $('.order1').removeClass('layui-this');
          $('.order2').addClass('layui-this');
          element.tabChange('orderListType', 'order2');
        }else{
          $('.order1').addClass('layui-this');
          $('.order2').removeClass('layui-this');
          element.tabChange('orderListType', 'order1');
        }
        //全部
        index.orderList('','#orderList1');
        //代付款
        index.orderList('INIT','#orderList2');
        //已支付
        index.orderList('SUCCESS','#orderList3');
        //已取消
        index.orderList('CANCEL','#orderList4');
    });
  },
  orderList:function(state,id){
    layui.use(['flow'], function(){
      var flow = layui.flow;
          //分页
          flow.load({
            elem: id, 
            done: function(page, next){
              var lis = [];
              $.ajax({
                type: 'post',
                url: link + '/mobile/order/list',
                data: {
                    'token':GetCookie('token'),
                    'pageNum':page,
                    'state':state?state:''
                },
                dataType: 'json',
                success: function (res) {
                    if(res.code==10000){
                      let orderListhtml = '';
                      if(res.entity.list.length>0){
                        layui.each(res.entity.list, function(index, item){
                          orderListhtml+='<div class="layui-col-xs12 layui-row pb10 pt10 borderBottomDdd">'+
                                            '<p class="font10 layui-col-xs7 h20 line20">'+
                                                '<span class="color666">订单编号:</span>'+
                                                '<span class="color999">'+item.orderNo+'</span>'+
                                            '</p>'+
                                            '<p class="layui-col-xs5 font10 h20 line20 layui-row">';
                                                if(item.orderState=='INIT'){
                                                  orderListhtml+='<span class="pointer colorRed goPayAgain layui-col-xs9 alignR" name="'+item.id+'">去支付</span>'+
                                                                  '<span class="pointer color666 goCancleOrder layui-col-xs3 alignR" name="'+item.id+'">取消</span>';
                                                }else if(item.orderState=='SUCCESS'){
                                                  orderListhtml+='<span class="pointer colorRed goPayAgain layui-col-xs12 alignR" name="'+item.id+'">已支付</span>';
                                                }else if(item.orderState=='CANCEL'){
                                                  orderListhtml+='<span class="pointer colorRed layui-col-xs12 alignR">已取消</span>';
                                                }
                                                orderListhtml+='</p>'+
                                            '<div class="layui-row mt10 layui-col-xs12">'+
                                                '<div class="layui-col-xs3 pr10">'+
                                                    '<img src="../../images/detail/banner.jpg" alt="" class="width80 h80">'+
                                                '</div>'+
                                                '<div class="layui-col-xs6 font12 pl5">'+
                                                    '<p class="color333">创标国际TM 四川高山生态红茶</p>'+
                                                    '<p class="color999 mt5">稀有老茶树、纯手工</p>'+
                                                    '<p class="color999 mt10 pl3">x 1</p>'+
                                                '</div>'+
                                                '<div class="layui-col-xs3 alignR">'+
                                                    '<p class="colorRed h80 line80">￥ 2000.00</p>'+
                                                '</div>'+
                                            '</div>'+
                                            '<p class="font10 layui-col-xs7 h20 line20 mt10">'+
                                                '<span class="color666">下单时间：</span>'+
                                                '<span class="color999">'+item.createTime+'</span>'+
                                            '</p>'+
                                            '<p class="layui-col-xs5 font10 h20 line20 layui-row mt10">'+
                                                '<span class="color666 layui-col-xs12 alignR">合计：￥ 2000.00</span>'+
                                            '</p>'+
                                        '</div>';
                        });
                        lis.push(orderListhtml);
                        next(lis.join(''), page < Math.ceil(res.entity.total/10));
                        $('.layui-flow-more').addClass('layui-col-xs12');
                      }else{
                       $(id).html('<p class="colorSystem alignC mt30 mb30">暂无订单</p>');
                      }
                    }else{
                        layer.msg(res.message);
                    }
                },
                error: function (res) {
                  layer.msg(res.message);
                },
              });
            }
          });
    });
    
  },
  //重新支付
  goPayAgain:function(){
    $('body').on('click','.goPayAgain',function(){
      let needOrderId = $(this).attr('name');
      $.ajax({
          url: link+'/mobile/order/toPay',
          type: 'post',
          dataType: 'json',
          data: {
            'token':GetCookie('token'),
            'orderId': needOrderId
          },
          success: function (res) {
            if(res.code==10000){
                window.location.href=res.entity;
            }else{
                layer.msg(res.message);
            }
          },
          error: function (error) {
              layer.msg('系统错误，订单删除失败',{icon:2})
          }
      });
    })
  },
  goCancleOrder:function(){
    $('body').on('click','.goCancleOrder',function(){
      let needOrderId = $(this).attr('name');
      layui.use('layer', function() {
        var $ = layui.jquery,
            layer = layui.layer;
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: true,
                shadeClose:true,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['确认','取消'],
                btnAlign: 'c',
                moveType: 1, //拖拽模式，0或者1
                content: '<div class="p20 font16 alignC pt40">是否确认取消该订单？</div>',
                yes: function(index,layero){
                    $.ajax({
                        url: link+'/mobile/order/cancel',
                        type: 'post',
                        dataType: 'json',
                        data: {
                          'token':GetCookie('token'),
                          'orderId': needOrderId
                        },
                        success: function (result) {
                          console.log(result);
                            if (result.code == 10000) {
                                layer.close(index);
                                window.location.reload();
                            } else {
                                layer.msg(result.message,{icon:2})
                            }
                        },
                        error: function (error) {
                            layer.msg('系统错误，订单删除失败',{icon:2})
                        }
                    });
                }
            });
        });
    })
  },
}
index.init();

          